<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>

  <script>
    // 封装
    const jsonp = ({ url, params, callback }) => {
      let script = document.createElement('script');
      let arrs = [];
      return new Promise((resolve, reject) => {
        window[callback] = function (data) {
          resolve(data)
          document.body.remove(script)
        }
        params = { ...params, callback }; // 处理 params 有多个参数值（格式：id:1&callback=callbackFunc）
        for (let key in params) {
          arrs.push(`${key}=${params[key]}`)
        }
        // console.log(arrs)
        script.src = `${url}?${arrs.join('&')}`
        document.body.appendChild(script)
      })
    }

    jsonp({
      url: "http://localhost:5555/users",
      // params: { id: 1 },
      callback: 'callbackFunc'
    }).then(data => {
      console.log(data)
    })
  </script>
</body>

</html>